/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poli.reserva.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import poli.reserva.bean.Login;


/**
 *
 * @author DAVID MOTTA
 */
public class JLoginMySQL {
    
    public List<Login> selectJusuarioMySQL(){
        
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
        ArrayList resultado = new ArrayList();
        
        
        try {
        
            
         String query = "SELECT * FROM login ;";   
        
        st = conn.createStatement();   
        
         rs = st.executeQuery(query);   
         
         rs.beforeFirst();
           
            while (rs.next())
      {
           Login user = new Login();
           user.setCedula(rs.getString(1));
           user.setNombre(rs.getString(2));
           user.setApellido(rs.getString(3));
           user.setEmail(rs.getString(4));
           user.setPass(rs.getString(5));
           user.setIdusuario(rs.getString(6));
           
           resultado.add(user);
                }
            
            st.close();
           }
           
       
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        return resultado;
        
    }
    
    public List<Login> selectJusuarioMySQLIdusuario(String idusuario){
        
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
        ArrayList resultado = new ArrayList();
        
        
        try {
        
            
         String query = "SELECT * FROM login where idusuario = "+"'"+idusuario+"' ;";   
        
        st = conn.createStatement();   
        
         rs = st.executeQuery(query);   
           
            while (rs.next())
      {
           Login user = new Login();
           user.setCedula(rs.getString(1));
           user.setNombre(rs.getString(2));
           user.setApellido(rs.getString(3));
           user.setEmail(rs.getString(4));
           user.setPass(rs.getString(5));
           user.setIdusuario(rs.getString(6));
           
          resultado.add(user);
                }
            
            st.close();
           }
           
       
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        return resultado;
        
    }
    
    
        public Boolean insertJusuarioMySQL(Login Jusuario){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
         
         boolean result = false;
          try {
        
            
         String query = "INSERT INTO reservas.login (cedula, nombre, apellido, email, pass ,idusuario)"+
                 " VALUES ( '"+Jusuario.getCedula()+"' , '"+Jusuario.getNombre()+"' , '"+Jusuario.getApellido()+"' , '"+Jusuario.getEmail()+"' , '"+Jusuario.getPass()+"', '"+Jusuario.getIdusuario()+"' );";   
        
        st = conn.createStatement();
        
        result =  st.execute(query);
        
            conn.close();     
            st.close();
           }
           
      
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        
         if (result!= true){
             
             return result;
         }
         
         else
             
        return true;
        
    }
        
        public Boolean updateJusuarioMySQL(String cedula , String nuevoNombre){
        
         Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
         
         boolean result = false;
          try {
        
            
      
              String query ="UPDATE LOGIN SET NOMBRE =  ? WHERE CEDULA = ? ";
        
        PreparedStatement preparedStmt = conn.prepareStatement(query);
        
        
      preparedStmt.setString(1, nuevoNombre);
      preparedStmt.setString(2, cedula);
        
         preparedStmt.executeUpdate();
        
            conn.close();     
            
           }
           
      
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        
         if (result!= false){
             
             return result;
         }
         
         else
             
        return true;
               
        
    }
        
            public Boolean deleteJusuarioMySQL(String cedula){
        
         Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
         
         boolean result = false;
          try {
        
            
       
              String query ="DELETE FROM LOGIN WHERE CEDULA = ?  ";
        
        PreparedStatement preparedStmt = conn.prepareStatement(query);
        
        
      preparedStmt.setString(1, cedula);
      
        
         preparedStmt.executeUpdate();
        
            conn.close();     
            
           }
           
      
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        
         if (result!= false){
             
             return result;
         }
         
         else
             
        return true;
        
    }




    
    
    
}
